'use strict';

goog.provide('Blockly.Arduino.mago');

goog.require('Blockly.Arduino');
goog.require('Blockly.JavaScript');

/********************************************
BASE - 基础模块
*********************************************/


Blockly.Arduino.mg_base_loop = function () {
  var branch = Blockly.Arduino.statementToCode(this, 'DO');
  branch = branch.replace(/(^\s*)|(\s*$)/g, "");//去除两端空格
  return branch;
};
Blockly.Arduino.mg_base_setup = function () {
  var branch = Blockly.Arduino.statementToCode(this, 'DO');
  branch = branch.replace(/(^\s*)|(\s*$)/g, "");//去除两端空格
  if (branch) {
    Blockly.Arduino.setups_['setup_setup'] = branch;
  }
  return '';
};
// PWM
Blockly.Arduino.mg_pwm = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var value_num = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
  var code="";
  var board_type = JSFuncs.getPlatform();
  if (window.isNaN(dropdown_pin) && board_type.match(RegExp(/STM32/)) == null) {
      code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
  } else {
      if (Blockly.Arduino.setups_['setup_input_' + dropdown_pin])
          delete Blockly.Arduino.setups_['setup_input_' + dropdown_pin];
      Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  }
  code += 'analogWrite(' + dropdown_pin + ',' + value_num + ');\n'
  return code;
};

Blockly.Arduino.controls_delay_second = function () {
  var delay_time = Blockly.Arduino.valueToCode(this, 'DELAY_TIME', Blockly.Arduino.ORDER_ATOMIC) || '1'
  var code = 'delay(' + delay_time*1000 + ');\n';
  return code;
};

// Buzzer active
Blockly.Arduino.mg_buzzer = Blockly.Arduino.yf_led;

// Buzzer Passive
Blockly.Arduino.yf_buzzer2 = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var fre = Blockly.Arduino.valueToCode(this, 'FREQUENCY',
    Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
  var code = "tone(" + dropdown_pin + "," + fre + ");\n";
  Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  return code;
};
// Buzzer Passive
Blockly.Arduino.mg_buzzer3 = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var fre = Blockly.Arduino.valueToCode(this, 'FREQUENCY',
    Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
  var dur = Blockly.Arduino.valueToCode(this, 'DURATION',
    Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
  var code = "tone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
  Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  return code;
};

// Speaker
Blockly.Arduino.yf_speaker = Blockly.Arduino.yf_buzzer2;
Blockly.Arduino.yf_speaker2 = Blockly.Arduino.yf_buzzer3;


// DHT11 22
Blockly.Arduino.yf_dht = function () {
  var sensor_type = this.getFieldValue('TYPE');
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var what = this.getFieldValue('WHAT');
  Blockly.Arduino.definitions_['define_dht'] = '#include <dht.h>';
  Blockly.Arduino.definitions_['var_dht_' + dropdown_pin] = 'dht myDHT_' + dropdown_pin + ';';
  var funcName = 'dht_' + dropdown_pin + '_get' + what;
  var code = 'int' + ' ' + funcName + '() {\n'
    + '  int chk = myDHT_' + dropdown_pin + '.read' + sensor_type + '(' + dropdown_pin + ');\n'
    + '  int value = myDHT_' + dropdown_pin + '.' + what + ';\n'
    + '  return value;\n'
    + '}\n';
  Blockly.Arduino.definitions_[funcName] = code;
  return [funcName + '()', Blockly.Arduino.ORDER_ATOMIC];
}


// 微动开关
Blockly.Arduino.mg_microswitch = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT_PULLUP);';
  var code = 'digitalRead(' + dropdown_pin + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}

// 触摸
Blockly.Arduino.mg_touchsensor = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var code = 'digitalRead(' + dropdown_pin + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}
// 红外避障
Blockly.Arduino.mg_Infraredsensor = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var code = 'digitalRead(' + dropdown_pin + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}
// 光敏
Blockly.Arduino.mg_lightsensor = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var code = 'digitalRead(' + dropdown_pin + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}
// 人体感应
Blockly.Arduino.mg_bodysensor = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var code = 'digitalRead(' + dropdown_pin + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}
// 雨滴
Blockly.Arduino.mg_rainsensor = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var code = 'digitalRead(' + dropdown_pin + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
}




// ledon
Blockly.Arduino.mg_ledon = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var code="";
  var board_type = JSFuncs.getPlatform();
  if (window.isNaN(dropdown_pin) && board_type.match(RegExp(/STM32/)) == null) {
      code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
  } else {
      if (Blockly.Arduino.setups_['setup_input_' + dropdown_pin])
          delete Blockly.Arduino.setups_['setup_input_' + dropdown_pin];
      Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  }
  code += 'digitalWrite(' + dropdown_pin + ',' + 'HIGH);\n'
  return code;
};
// ledoff
Blockly.Arduino.mg_ledoff = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var code="";
  var board_type = JSFuncs.getPlatform();
  if (window.isNaN(dropdown_pin) && board_type.match(RegExp(/STM32/)) == null) {
      code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
  } else {
      if (Blockly.Arduino.setups_['setup_input_' + dropdown_pin])
          delete Blockly.Arduino.setups_['setup_input_' + dropdown_pin];
      Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  }
  code += 'digitalWrite(' + dropdown_pin + ',' + 'LOW);\n'
  return code;
};

// ledpwm
Blockly.Arduino.mg_ledpwm = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var speed = Blockly.Arduino.valueToCode(this, 'speed', Blockly.Arduino.ORDER_ATOMIC);
  var code="";
  var board_type = JSFuncs.getPlatform();
  if (window.isNaN(dropdown_pin) && board_type.match(RegExp(/STM32/)) == null) {
      code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
  } else {
      if (Blockly.Arduino.setups_['setup_input_' + dropdown_pin])
          delete Blockly.Arduino.setups_['setup_input_' + dropdown_pin];
      Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  }
  code += 'analogWrite(' + dropdown_pin + ',' + speed + ');\n'
  return code;
};

// 蜂鸣器on
Blockly.Arduino.mg_buzzeron = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var code="";
  var board_type = JSFuncs.getPlatform();
  if (window.isNaN(dropdown_pin) && board_type.match(RegExp(/STM32/)) == null) {
      code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
  } else {
      if (Blockly.Arduino.setups_['setup_input_' + dropdown_pin])
          delete Blockly.Arduino.setups_['setup_input_' + dropdown_pin];
      Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  }
  code += 'digitalWrite(' + dropdown_pin + ',' + 'HIGH);\n'
  return code;
};
// 蜂鸣器off
Blockly.Arduino.mg_buzzeroff = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var code="";
  var board_type = JSFuncs.getPlatform();
  if (window.isNaN(dropdown_pin) && board_type.match(RegExp(/STM32/)) == null) {
      code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
  } else {
      if (Blockly.Arduino.setups_['setup_input_' + dropdown_pin])
          delete Blockly.Arduino.setups_['setup_input_' + dropdown_pin];
      Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  }
  code += 'digitalWrite(' + dropdown_pin + ',' + 'LOW);\n'
  return code;
};


// 单个电机
Blockly.Arduino.mg_singlemotor = function () {
  var pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_dir = this.getFieldValue('DIR');
  Blockly.Arduino.setups_['setup_output_' + pin1] = 'pinMode(' + pin1 + ', OUTPUT);';
  Blockly.Arduino.setups_['setup_output_' + pin2] = 'pinMode(' + pin2 + ', OUTPUT);';
  if ("1" == dropdown_dir) {
    var code = 'digitalWrite(' + pin1 + ',' + 'HIGH);\n'
      + 'digitalWrite(' + pin2 + ',' + 'LOW);\n'
  }else{
    var code = 'digitalWrite(' + pin1 + ',' + 'LOW);\n'
      + 'digitalWrite(' + pin2 + ',' + 'HIGH);\n'
  }
  return code;
};
// 单个电机
Blockly.Arduino.mg_singlemotor_stop = function () {
  var pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_dir = this.getFieldValue('DIR');
  Blockly.Arduino.setups_['setup_output_' + pin1] = 'pinMode(' + pin1 + ', OUTPUT);';
  Blockly.Arduino.setups_['setup_output_' + pin2] = 'pinMode(' + pin2 + ', OUTPUT);';
  if ("1" == dropdown_dir) {
    var code = 'digitalWrite(' + pin1 + ',' + 'LOW);\n'
      + 'digitalWrite(' + pin2 + ',' + 'LOW);\n'
  }else{
    var code = 'digitalWrite(' + pin1 + ',' + 'LOW);\n'
      + 'digitalWrite(' + pin2 + ',' + 'LOW);\n'
  }
  return code;
};
// 单个电机
Blockly.Arduino.mg_singlemotorpwm1 = function () {
  var pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_dir = this.getFieldValue('DIR');
  var speed = Blockly.Arduino.valueToCode(this, 'speed', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_output_' + pin1] = 'pinMode(' + pin1 + ', OUTPUT);';
  Blockly.Arduino.setups_['setup_output_' + pin2] = 'pinMode(' + pin2 + ', OUTPUT);';
  Blockly.Arduino.setups_['setup_low_' + pin1] = 'digitalWrite(' + pin1 + ', LOW);';
  Blockly.Arduino.setups_['setup_low_' + pin2] = 'digitalWrite(' + pin2 + ', LOW);';
  if ("1" == dropdown_dir) {
    var code = 'analogWrite(' + pin1 + ',' + speed + ');\n'
      + 'digitalWrite(' + pin2 + ',' + 'HIGH);\n'
  } else {
    var code = 'analogWrite(' + pin1 + ',' + speed + ');\n'
      + 'digitalWrite(' + pin2 + ',' + 'LOW);\n'
  }
  return code;
};

// 单个电机
Blockly.Arduino.mg_singlemotorpwm2 = function () {
  var pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_output_' + pin1] = 'pinMode(' + pin1 + ', OUTPUT);';
  Blockly.Arduino.setups_['setup_output_' + pin2] = 'pinMode(' + pin2 + ', OUTPUT);';
  var code = 'digitalWrite(' + pin1 + ',' + 'LOW);\n'
    + 'digitalWrite(' + pin2 + ',' + 'LOW);\n'
  return code;
};


// motor1on
Blockly.Arduino.mg_motor1on = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  var code = 'digitalWrite(' + dropdown_pin + ',' + 'HIGH);\n'
  return code;
};
// motor1off
Blockly.Arduino.mg_motor1off = function () {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  var code = 'digitalWrite(' + dropdown_pin + ',' + 'LOW);\n'
  return code;
};

// motor1pwm
Blockly.Arduino.mg_motor1pwm = function () {
  var pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_dir = this.getFieldValue('DIR');
  var speed = Blockly.Arduino.valueToCode(this, 'speed', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_output_' + pin1] = 'pinMode(' + pin1 + ', OUTPUT);';
  Blockly.Arduino.setups_['setup_output_' + pin2] = 'pinMode(' + pin2 + ', OUTPUT);';
  Blockly.Arduino.setups_['setup_low_' + pin1] = 'digitalWrite(' + pin1 + ', LOW);';
  Blockly.Arduino.setups_['setup_low_' + pin2] = 'digitalWrite(' + pin2 + ', LOW);';
  if ("1" == dropdown_dir) {
    var code = 'analogWrite(' + pin1 + ',' + speed + ');\n'
      + 'digitalWrite(' + pin2 + ',' + 'HIGH);\n'
  } else {
    var code = 'analogWrite(' + pin1 + ',' + speed + ');\n'
      + 'digitalWrite(' + pin2 + ',' + 'LOW);\n'
  }
  return code;
};





//舵机
Blockly.Arduino.mg_varspeedservo = function () {
  var dropdown_pin = this.getFieldValue('PIN');
  var value_degree = Blockly.Arduino.valueToCode(this, 'DEGREE', Blockly.Arduino.ORDER_ATOMIC) || '0';
  var value_speed = Blockly.Arduino.valueToCode(this, 'SPEED', Blockly.Arduino.ORDER_ATOMIC) || '0'
  Blockly.Arduino.definitions_['include_Servo'] = '#include <VarSpeedServo.h>';
  Blockly.Arduino.definitions_['var_declare_servo' + dropdown_pin] = 'VarSpeedServo varSpeedServo_' + dropdown_pin + ';';
  Blockly.Arduino.setups_['setup_servo_' + dropdown_pin] = 'varSpeedServo_' + dropdown_pin + '.attach(' + dropdown_pin + ');';
  //varSpeedServo_.write(DEGREE,SPEED)#角度,速度
  var code = 'varSpeedServo_' + dropdown_pin + '.write(' + value_degree + ',' + value_speed + ');\n';
  return code;
};
Blockly.Arduino.mg_varspeedservobywait = function () {
  var dropdown_pin = this.getFieldValue('PIN');
  //是否等待舵机运行完成
  var code = 'varSpeedServo_' + dropdown_pin + '.wait();\n';
  return code;
};
Blockly.Arduino.mg_varspeedservobystop = function () {
  var dropdown_pin = this.getFieldValue('PIN');
  //将舵机停在当前位置
  var code = 'varSpeedServo_' + dropdown_pin + '.stop();\n';
  return code;
};


// 读取舵机度数
Blockly.Arduino.mg_varspeedservobydegree = function () {
  var dropdown_pin = this.getFieldValue('PIN');
  var code = 'varSpeedServo_' + dropdown_pin + '.read()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};




Blockly.Arduino.make_tool_normal = function () {
  var value_data = Blockly.Arduino.valueToCode(this, 'data', Blockly.Arduino.ORDER_ATOMIC);
  var code = value_data + ';\n';
  return code;
};

//初始化舵机
Blockly.Arduino.make_arduino_servo_begin = function () {
  this.setTooltip("初始化舵机，返回数据的类型为boolean，成功返回通道号，失败则返回0");
  var text_name = this.getFieldValue('name');
  var value_pin = Blockly.Arduino.valueToCode(this, 'pin', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_' + 'VarSpeedServo'] = '#include <VarSpeedServo.h>';
  Blockly.Arduino.definitions_['var_declare_' + text_name] = 'VarSpeedServo ' + text_name + ';';
  //Blockly.Arduino.setups_['varspeedservo_'+text_name] = text_name+'.attach('+value_pin+');';
  var code = text_name + '.attach(' + value_pin + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//初始化舵机 可设置最小角度和最大角度
Blockly.Arduino.make_arduino_servo_begin_1 = function () {
  this.setTooltip("初始化舵机，返回数据的类型为boolean，成功返回通道号，失败则返回0");
  var text_name = this.getFieldValue('name');
  var value_pin = Blockly.Arduino.valueToCode(this, 'pin', Blockly.Arduino.ORDER_ATOMIC);
  var value_min = Blockly.Arduino.valueToCode(this, 'min', Blockly.Arduino.ORDER_ATOMIC);
  var value_max = Blockly.Arduino.valueToCode(this, 'max', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_' + 'VarSpeedServo'] = '#include <VarSpeedServo.h>';
  Blockly.Arduino.definitions_['var_declare_' + text_name] = 'VarSpeedServo ' + text_name + ';';
  //Blockly.Arduino.setups_['varspeedservo_'+text_name] = text_name+'.attach('+value_pin+', '+value_min+', '+value_max+');';
  var code = text_name + '.attach(' + value_pin + ', ' + value_min + ', ' + value_max + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//舵机 设置角度
Blockly.Arduino.make_arduino_servo_write = function () {
  this.setTooltip("舵机 设置当前的角度(0~180°)，输入值超出544视为脉冲宽度，小于则视为角度，不阻塞程序运行");
  var text_name = this.getFieldValue('name');
  var value_value = Blockly.Arduino.valueToCode(this, 'value', Blockly.Arduino.ORDER_ATOMIC);
  var code = text_name + '.write(' + value_value + ');\n';
  return code;
};

//舵机 设置角度和速度
Blockly.Arduino.make_arduino_servo_write_1 = function () {
  this.setTooltip("舵机 设置当前的角度(0~180°)，输入值超出544视为脉冲宽度，小于则视为角度，不阻塞程序运行");
  var text_name = this.getFieldValue('name');
  var value_value = Blockly.Arduino.valueToCode(this, 'value', Blockly.Arduino.ORDER_ATOMIC);
  var value_speed = Blockly.Arduino.valueToCode(this, 'speed', Blockly.Arduino.ORDER_ATOMIC);
  var code = text_name + '.write(' + value_value + ', ' + value_speed + ');\n';
  return code;
};

//舵机 设置角度和速度以及选择是否阻塞
Blockly.Arduino.make_arduino_servo_write_2 = function () {
  this.setTooltip("舵机 设置当前的角度(0~180°)，输入值超出544视为脉冲宽度，小于则视为角度");
  var text_name = this.getFieldValue('name');
  var value_value = Blockly.Arduino.valueToCode(this, 'value', Blockly.Arduino.ORDER_ATOMIC);
  var value_speed = Blockly.Arduino.valueToCode(this, 'speed', Blockly.Arduino.ORDER_ATOMIC);
  var value_wait = Blockly.Arduino.valueToCode(this, 'wait', Blockly.Arduino.ORDER_ATOMIC);
  var code = text_name + '.write(' + value_value + ', ' + value_speed + ', ' + value_wait + ');\n';
  return code;
};

//舵机 设置脉冲宽度
Blockly.Arduino.make_arduino_servo_writeMicroseconds = function () {
  this.setTooltip("舵机 设置脉冲宽度，默认的最小脉宽为544微秒，最大脉宽为2400微秒");
  var value_value = Blockly.Arduino.valueToCode(this, 'value', Blockly.Arduino.ORDER_ATOMIC);
  var text_name = this.getFieldValue('name');
  var code = text_name + '.writeMicroseconds(' + value_value + ');\n';
  return code;
};

//舵机 获取数据
Blockly.Arduino.make_arduino_servo_read = function () {
  var text_name = this.getFieldValue('name');
  var dropdown_type = this.getFieldValue('type');
  if (dropdown_type == 'read')
    this.setTooltip("舵机 获取当前角度，返回数据的类型为int");
  else if (dropdown_type == 'readMicroseconds')
    this.setTooltip("舵机 获取当前脉宽，返回数据的类型为int");
  else if (dropdown_type == 'isMoving')
    this.setTooltip("舵机 正在移动?，返回数据的类型为boolean");
  else
    this.setTooltip("舵机 舵机连上控制管脚?，返回数据的类型为boolean");
  var code = text_name + '.' + dropdown_type + '()';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//舵机 执行某些函数
Blockly.Arduino.make_arduino_servo_set = function () {
  var text_name = this.getFieldValue('name');
  var dropdown_type = this.getFieldValue('type');
  if (dropdown_type == 'stop')
    this.setTooltip("舵机 停止移动");
  else if (dropdown_type == 'wait')
    this.setTooltip("舵机 等待移动完成");
  else if (dropdown_type == 'sequenceStop')
    this.setTooltip("舵机 停止执行动作组");
  else
    this.setTooltip("舵机 释放管脚");
  var code = text_name + '.' + dropdown_type + '();\n';
  return code;
};

//舵机 建立一个动作组
Blockly.Arduino.make_arduino_servo_action_group = function () {
  var text_name = this.getFieldValue('name');
  var statements_data = Blockly.Arduino.statementToCode(this, 'data');
  if (statements_data)
    statements_data = statements_data.substring(2, statements_data.length - 1);
  Blockly.Arduino.definitions_['include_' + 'VarSpeedServo'] = '#include <VarSpeedServo.h>';
  Blockly.Arduino.definitions_['var_declare_' + text_name] = 'servoSequencePoint ' + text_name + '[] = {' + statements_data + '};';
  var code = '';
  return code;
};

//舵机 动作组里数据定义
Blockly.Arduino.make_arduino_servo_action_group_data = function () {
  var text_angle = this.getFieldValue('angle');
  var text_speed = this.getFieldValue('speed');
  var surround_parent = this.getSurroundParent();
  var code = '';
  if (surround_parent && surround_parent.type == 'make_arduino_servo_action_group') {
    this.setWarningText(null);
    code = '{' + text_angle + ',' + text_speed + '},';
  }
  else {
    this.setWarningText("此块需放到创建动作组块下");
  }
  return code;
};

//舵机 循环执行某一动作组
Blockly.Arduino.make_arduino_servo_sequencePlay = function () {
  this.setTooltip("舵机 从索引为0的动作开始，循环执行某一动作组里的动作，返回数据的类型为uint8_t");
  var text_name = this.getFieldValue('name');
  var value_sequencein = Blockly.Arduino.valueToCode(this, 'sequencein', Blockly.Arduino.ORDER_ATOMIC);
  var value_numpositions = Blockly.Arduino.valueToCode(this, 'numpositions', Blockly.Arduino.ORDER_ATOMIC);
  var code = text_name + '.sequencePlay(' + value_sequencein + ', ' + value_numpositions + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//舵机 执行某一动作组，可设置是否循环
Blockly.Arduino.make_arduino_servo_sequencePlay_1 = function () {
  this.setTooltip("舵机 从某个索引的动作开始，执行某一动作组里的动作，返回数据的类型为uint8_t");
  var text_name = this.getFieldValue('name');
  var value_sequencein = Blockly.Arduino.valueToCode(this, 'sequencein', Blockly.Arduino.ORDER_ATOMIC);
  var value_numpositions = Blockly.Arduino.valueToCode(this, 'numpositions', Blockly.Arduino.ORDER_ATOMIC);
  var value_loop = Blockly.Arduino.valueToCode(this, 'loop', Blockly.Arduino.ORDER_ATOMIC);
  var value_startPos = Blockly.Arduino.valueToCode(this, 'startPos', Blockly.Arduino.ORDER_ATOMIC);
  var code = text_name + '.sequencePlay(' + value_sequencein + ', ' + value_numpositions + ', ' + value_loop + ', ' + value_startPos + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//for
Blockly.Arduino.mg_make_for = function () {
  var value_variate = Blockly.Arduino.valueToCode(this, 'variate', Blockly.Arduino.ORDER_ATOMIC);
  var value_judge = Blockly.Arduino.valueToCode(this, 'judge', Blockly.Arduino.ORDER_ATOMIC);
  var value_operation = Blockly.Arduino.valueToCode(this, 'operation', Blockly.Arduino.ORDER_ATOMIC);
  var statements_do = Blockly.Arduino.statementToCode(this, 'do');  
  var code = 'for('+value_variate+'; '+value_judge+'; '+value_operation+')\n'
            +'{\n'
            +''+statements_do
            +'}\n';
  return code;
};


// PS2 Controller init
Blockly.Arduino.mg_ps2_init = function () {
  var PS2_DAT = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC || '10');
  var PS2_CMD = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC || '11');
  var PS2_CS = Blockly.Arduino.valueToCode(this, 'PIN3', Blockly.Arduino.ORDER_ATOMIC || '12');
  var PS2_CLK = Blockly.Arduino.valueToCode(this, 'PIN4', Blockly.Arduino.ORDER_ATOMIC || '13');
  Blockly.Arduino.definitions_['include_ps2'] = '#include <PS2X_lib.h>';
  Blockly.Arduino.definitions_['define_ps2_dat'] = '#define PS2_DAT ' + PS2_DAT;
  Blockly.Arduino.definitions_['define_ps2_cmd'] = '#define PS2_CMD ' + PS2_CMD;
  Blockly.Arduino.definitions_['define_ps2_cs'] = '#define PS2_CS ' + PS2_CS;
  Blockly.Arduino.definitions_['define_ps2_clk'] = '#define PS2_CLK ' + PS2_CLK;
  Blockly.Arduino.definitions_['var_ps2x'] = 'PS2X ps2x;';
  Blockly.Arduino.definitions_['var_error'] = 'int error = 0;';
  Blockly.Arduino.definitions_['var_type'] = 'byte type = 0;';
  Blockly.Arduino.definitions_['var_vibrate'] = 'byte vibrate = 0;';

  Blockly.Arduino.setups_['setup_init_ps2'] = 'ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_CS, PS2_DAT, true, true);\n';
  Blockly.Arduino.setups_['setup_init_ps2_type'] = 'ps2x.readType();\n';
  return '';

};

// PS2 Controller button
Blockly.Arduino.mg_ps2_btn = function () {
  var PS2_BTN = Blockly.Arduino.variableDB_.getName(this.getFieldValue('PS2_BTN'), Blockly.Variables.NAME_TYPE);
  var PS2_BTN_STATUS = Blockly.Arduino.variableDB_.getName(this.getFieldValue('PS2_BTN_STATUS'), Blockly.Variables.NAME_TYPE);
  var code = 'ps2x.' + PS2_BTN_STATUS + '(' + PS2_BTN + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

// PS2 Controller Rocker
Blockly.Arduino.mg_ps2_rocker = function () {
  // var PS2_ROCKER_STATUS = this.getFieldValue('PS2_ROCKER_STATUS');
  var PS2_ROCKER_STATUS = Blockly.Arduino.variableDB_.getName(this.getFieldValue('PS2_ROCKER_STATUS'), Blockly.Variables.NAME_TYPE);
  var code = 'ps2x.Analog(' + PS2_ROCKER_STATUS + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

// PS2 Controller read analog value of the button  --- how hard you press the button
Blockly.Arduino.mg_ps2_a_btn = function () {
  var PS2_A_BTN = this.getFieldValue('PS2_A_BTN');
  var code = 'ps2x.Analog(' + PS2_A_BTN + ')';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

// PS2 Controller read controller and setmotor
Blockly.Arduino.mg_ps2_readController_setMotoron = function () {
  var PS2_Motor1 = 'true';
  var PS2_Motor2 = '1';
  var code = 'ps2x.read_gamepad(' + PS2_Motor1 + ',' + PS2_Motor2 + ');    //开启震动必须在后面加延时50ms   \n '
  return code;
};
Blockly.Arduino.mg_ps2_readController_setMotoroff = function () {
  var PS2_Motor1 = 'false';
  var PS2_Motor2 = '0';
  var code = 'ps2x.read_gamepad(' + PS2_Motor1 + ',' + PS2_Motor2 + ');\n'
  return code;
};

  // DHT11 22
  Blockly.Arduino.mg_dht = function () {
    var sensor_type = this.getFieldValue('TYPE');
    var dropdown_pin = this.getFieldValue('PIN');
    var what = this.getFieldValue('WHAT');
    Blockly.Arduino.definitions_['define_dht'] = '#include <dht.h>';
    Blockly.Arduino.definitions_['define_dht_' + dropdown_pin] = 'dht myDHT_' + dropdown_pin + ';';
    var funcName = 'dht_' + dropdown_pin + '_get' + what;
    var code = 'int' + ' ' + funcName + '() {\n'
      + '  int chk = myDHT_' + dropdown_pin + '.read' + sensor_type + '(' + dropdown_pin + ');\n'
      + '  int value = myDHT_' + dropdown_pin + '.' + what + ';\n'
      + '  return value;\n'
      + '}\n';
    Blockly.Arduino.definitions_[funcName] = code;
    return [funcName + '()', Blockly.Arduino.ORDER_ATOMIC];
  }

//TODO 智慧家居
//RGB
Blockly.Arduino.mg_rgb2on = function () {
  var pin = "A4";  //引脚
  var value_ledcount = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_time = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  var colour_rgb_led_color = this.getFieldValue('RGB_LED_COLOR');
  Blockly.Arduino.definitions_['include_display'] = '#include "Adafruit_NeoPixel.h"';
  Blockly.Arduino.definitions_['var_rgb_display'] = 'Adafruit_NeoPixel  rgb_display' + '(' + value_ledcount + ',' + pin + ');';
  Blockly.Arduino.setups_['setup_rgb_displaybegin_'] = 'rgb_display.begin();';
  Blockly.Arduino.setups_['setup_rgb_display_initshow'] = 'rgb_display.show();  // Initialize all pixels to off';

  var color = goog.color.hexToRgb(colour_rgb_led_color);
  var code = 'for( int i = 0 ;' + 'i < ' + (value_ledcount) + '; i++ )\n'
    + '{\n'
    + '  rgb_display' + '.setPixelColor(i, ' + color + ');\n'
    + '  delay(' + dropdown_time + ');\n'
    + '  rgb_display' + '.show();\n'
    + '}\n';
  return code;
};

Blockly.Arduino.mg_rgb2off = function () {
  var value_ledcount = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_time = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);

  var code = 'for( int i = ' + value_ledcount + ' ;' + 'i >= 0 ; i-- )\n'
    + '{\n'
    + '  rgb_display' + '.setPixelColor(i, 0,0,0);\n'
    + '  delay(' + dropdown_time + ');\n'
    + '  rgb_display' + '.show();\n'
    + '}\n';
  return code;
};
Blockly.Arduino.mg_rgb2on_low = function () {
  var pin = "A4";  //引脚
  var value_ledcount = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_type = this.getFieldValue('SPEED');
  var dropdown_color = this.getFieldValue('COLOR');
  var type = 'delay(50);\n';
  if (dropdown_type == 'low')
    type = 'delay(150);\n';
  else if (dropdown_type == 'middle')
    type = 'delay(100);\n';
  else if (dropdown_type == 'fast') {
    type = 'delay(50);\n';
  }

  Blockly.Arduino.definitions_['include_display'] = '#include "Adafruit_NeoPixel.h"';
  Blockly.Arduino.definitions_['var_rgb_display'] = 'Adafruit_NeoPixel  rgb_display' + '(' + value_ledcount + ',' + pin + ');';
  Blockly.Arduino.setups_['setup_rgb_displaybegin_'] = 'rgb_display.begin();';
  Blockly.Arduino.setups_['setup_rgb_display_initshow'] = 'rgb_display.show();  // Initialize all pixels to off';

  var light_part = '  rgb_display.setPixelColor(j, i,i,i);\n';
  var show_part = '  rgb_display' + '.show();\n';
  if (dropdown_color == 'R')
    light_part = '  rgb_display.setPixelColor(j, i,0,0);\n';
  else if (dropdown_color == 'G')
    light_part = '  rgb_display.setPixelColor(j, 0,i,0);\n';
  else if (dropdown_color == 'B') {
    light_part = '  rgb_display.setPixelColor(j, 0,0,i);\n';
  } else if (dropdown_color == 'W') {
    light_part = '  rgb_display.setPixelColor(j, i,i,i);\n';
  }

  var code = 'for(int i = 0 ;' + 'i < 255; i++ )\n'
    + '{  \n'
    + '  for(int j = 0 ;' + 'j < ' + (value_ledcount) + '; j++ )\n'
    + '{  \n'
    + light_part
    + show_part
    + '  }\n'
    + type
    + '}\n';

  return code;
};
Blockly.Arduino.mg_rgb2off_low = function () {
  var value_ledcount = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_type = this.getFieldValue('SPEED');
  var dropdown_color = this.getFieldValue('COLOR');
  var type = 'delay(50);\n';
  if (dropdown_type == 'low')
    type = 'delay(150);\n';
  else if (dropdown_type == 'middle')
    type = 'delay(100);\n';
  else if (dropdown_type == 'fast') {
    type = 'delay(50);\n';
  }
  var light_part = '  rgb_display.setPixelColor(j, i,i,i);\n';
  var show_part = '  rgb_display' + '.show();\n';
  if (dropdown_color == 'R')
    light_part = '  rgb_display.setPixelColor(j, i,0,0);\n';
  else if (dropdown_color == 'G')
    light_part = '  rgb_display.setPixelColor(j, 0,i,0);\n';
  else if (dropdown_color == 'B') {
    light_part = '  rgb_display.setPixelColor(j, 0,0,i);\n';
  } else if (dropdown_color == 'W') {
    light_part = '  rgb_display.setPixelColor(j, i,i,i);\n';
  }

  var code = 'for(int i = 255 ;' + 'i >= 0; i-- )\n'
    + '{  \n'
    + '  for(int j = 0 ;' + 'j < ' + (value_ledcount) + '; j++ )\n'
    + '{  \n'
    + light_part
    + show_part
    + '  }\n'
    + type
    + '}\n';

  return code;
};



Blockly.Arduino.mg_raindrops = function () {

  Blockly.Arduino.setups_['setup_output_'] = 'pinMode(A2, INPUT);';

  var code = '!digitalRead(A2)';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.mg_lightcontrol = function () {

  Blockly.Arduino.setups_['setup_output_'] = 'pinMode(A1, INPUT);';

  var code = 'digitalRead(A1)';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.mg_touchmodule = function () {

  Blockly.Arduino.setups_['setup_output_'] = 'pinMode(A3, INPUT);';

  var code = 'digitalRead(A3)';
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};


//舵机 设置角度和速度
Blockly.Arduino.mg_servo_write_smarthome = function () {
  this.setTooltip("舵机 设置当前的角度(0~180°)，输入值超出544视为脉冲宽度，小于则视为角度，不阻塞程序运行");
  var text_name = this.getFieldValue('name');
  var value_pin = Blockly.Arduino.valueToCode(this, 'pin', Blockly.Arduino.ORDER_ATOMIC);
  var value_value = Blockly.Arduino.valueToCode(this, 'value', Blockly.Arduino.ORDER_ATOMIC);
  var value_speed = Blockly.Arduino.valueToCode(this, 'speed', Blockly.Arduino.ORDER_ATOMIC);


  Blockly.Arduino.definitions_['include_' + 'VarSpeedServo'] = '#include <VarSpeedServo.h>';
  Blockly.Arduino.definitions_['var_declare_' + text_name] = 'VarSpeedServo ' + text_name + ';';
  //Blockly.Arduino.setups_['varspeedservo_'+text_name] = text_name+'.attach('+value_pin+');';
  var code = text_name + '.attach(' + value_pin + ');\n';
  code += text_name + '.write(' + value_value + ', ' + value_speed + ');\n';
  code += text_name + '.wait();\n';
  code += text_name + '.detach();\n';
  return code;
};


//显示-MAX7219-初始化
Blockly.Arduino.mg_MAX7219_init = function() {
  var pin_din = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
  var pin_cs = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
  var pin_clk = Blockly.Arduino.valueToCode(this, 'PIN3', Blockly.Arduino.ORDER_ATOMIC);
  // var lc_num = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
  var lc_num = 1;
  var Intensity = Blockly.Arduino.valueToCode(this, 'Intensity', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['define0_MaxMatrix'] = '#include "MaxMatrix.h"';
  Blockly.Arduino.definitions_['define1_MaxMatrix'] = '#include <avr/pgmspace.h>';
  Blockly.Arduino.definitions_['define_LIST'] = 'PROGMEM const unsigned char LIST[]{\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B00000000,B00000000,B00000000,B00000000,B00000000,//space\n';
  Blockly.Arduino.definitions_['define_LIST'] += '1,8,B01011111,B00000000,B00000000,B00000000,B00000000,//!\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B00000011,B00000000,B00000011,B00000000,B00000000,// \n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00010100,B00111110,B00010100,B00111110,B00010100,//# \n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00100100,B01101010,B00101011,B00010010,B00000000,//$ \n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B01100011,B00010011,B00001000,B01100100,B01100011,//% \n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00110110,B01001001,B01010110,B00100000,B01010000,//& \n';
  Blockly.Arduino.definitions_['define_LIST'] += '1,8,B00000011,B00000000,B00000000,B00000000,B00000000,//\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B00011100,B00100010,B01000001,B00000000,B00000000,//( \n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B01000001,B00100010,B00011100,B00000000,B00000000,//) \n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00101000,B00011000,B00001110,B00011000,B00101000,//* \n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00001000,B00001000,B00111110,B00001000,B00001000,//+ \n';
  Blockly.Arduino.definitions_['define_LIST'] += '2,8,B10110000,B01110000,B00000000,B00000000,B00000000,//, \n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00001000,B00001000,B00001000,B00001000,B00000000,//- \n';
  Blockly.Arduino.definitions_['define_LIST'] += '2,8,B01100000,B01100000,B00000000,B00000000,B00000000,//. \n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01100000,B00011000,B00000110,B00000001,B00000000,/// \n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111110,B01000001,B01000001,B00111110,B00000000,//0\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B01000010,B01111111,B01000000,B00000000,B00000000,//1\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01100010,B01010001,B01001001,B01000110,B00000000,//2\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00100010,B01000001,B01001001,B00110110,B00000000,//3\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00011000,B00010100,B00010010,B01111111,B00000000,//4\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00100111,B01000101,B01000101,B00111001,B00000000,//5\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111110,B01001001,B01001001,B00110000,B00000000,//6\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01100001,B00010001,B00001001,B00000111,B00000000,//7\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00110110,B01001001,B01001001,B00110110,B00000000,//8\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00000110,B01001001,B01001001,B00111110,B00000000,//9\n';
  Blockly.Arduino.definitions_['define_LIST'] += '2,8,B01010000,B00000000,B00000000,B00000000,B00000000,//: \n';
  Blockly.Arduino.definitions_['define_LIST'] += '2,8,B10000000,B01010000,B00000000,B00000000,B00000000,//; \n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B00010000,B00101000,B01000100,B00000000,B00000000,//< \n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B00010100,B00010100,B00010100,B00000000,B00000000,//= \n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B01000100,B00101000,B00010000,B00000000,B00000000,//> \n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00000010,B01011001,B00001001,B00000110,B00000000,//? \n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00111110,B01001001,B01010101,B01011101,B00001110,//@ \n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111110,B00010001,B00010001,B01111110,B00000000,//A\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B01001001,B01001001,B00110110,B00000000,//B\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111110,B01000001,B01000001,B00100010,B00000000,//C\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B01000001,B01000001,B00111110,B00000000,//D\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B01001001,B01001001,B01000001,B00000000,//E\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B00001001,B00001001,B00000001,B00000000,//F\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111110,B01000001,B01001001,B01111010,B00000000,//G\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B00001000,B00001000,B01111111,B00000000,//H\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B01000001,B01111111,B01000001,B00000000,B00000000,//I\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00110000,B01000000,B01000001,B00111111,B00000000,//J\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B00001000,B00010100,B01100011,B00000000,//K\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B01000000,B01000000,B01000000,B00000000,//L\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B01111111,B00000010,B00001100,B00000010,B01111111,//M\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B01111111,B00000100,B00001000,B00010000,B01111111,//N\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111110,B01000001,B01000001,B00111110,B00000000,//O\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B00001001,B00001001,B00000110,B00000000,//P\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111110,B01000001,B01000001,B10111110,B00000000,//Q\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B00001001,B00001001,B01110110,B00000000,//R\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01000110,B01001001,B01001001,B00110010,B00000000,//S\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00000001,B00000001,B01111111,B00000001,B00000001,//T\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111111,B01000000,B01000000,B00111111,B00000000,//U\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00001111,B00110000,B01000000,B00110000,B00001111,//V\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00111111,B01000000,B00111000,B01000000,B00111111,//W\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B01100011,B00010100,B00001000,B00010100,B01100011,//X\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00000111,B00001000,B01110000,B00001000,B00000111,//Y\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01100001,B01010001,B01001001,B01000111,B00000000,//Z\n';
  Blockly.Arduino.definitions_['define_LIST'] += '2,8,B01111111,B01000001,B00000000,B00000000,B00000000,//[ \n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00000001,B00000110,B00011000,B01100000,B00000000,//\backslash\n';
  Blockly.Arduino.definitions_['define_LIST'] += '2,8,B01000001,B01111111,B00000000,B00000000,B00000000,//] \n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B00000010,B00000001,B00000010,B00000000,B00000000,//hat\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01000000,B01000000,B01000000,B01000000,B00000000,//_ \n';
  Blockly.Arduino.definitions_['define_LIST'] += '2,8,B00000001,B00000010,B00000000,B00000000,B00000000,//` \n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00100000,B01010100,B01010100,B01111000,B00000000,//a\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B01000100,B01000100,B00111000,B00000000,//b\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111000,B01000100,B01000100,B00101000,B00000000,//c\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111000,B01000100,B01000100,B01111111,B00000000,//d\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111000,B01010100,B01010100,B00011000,B00000000,//e\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B00000100,B01111110,B00000101,B00000000,B00000000,//f\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B10011000,B10100100,B10100100,B01111000,B00000000,//g\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B00000100,B00000100,B01111000,B00000000,//h\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B01000100,B01111101,B01000000,B00000000,B00000000,//i\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01000000,B10000000,B10000100,B01111101,B00000000,//j\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111111,B00010000,B00101000,B01000100,B00000000,//k\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B01000001,B01111111,B01000000,B00000000,B00000000,//l\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B01111100,B00000100,B01111100,B00000100,B01111000,//m\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111100,B00000100,B00000100,B01111000,B00000000,//n\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111000,B01000100,B01000100,B00111000,B00000000,//o\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B11111100,B00100100,B00100100,B00011000,B00000000,//p\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00011000,B00100100,B00100100,B11111100,B00000000,//q\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01111100,B00001000,B00000100,B00000100,B00000000,//r\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B01001000,B01010100,B01010100,B00100100,B00000000,//s\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B00000100,B00111111,B01000100,B00000000,B00000000,//t\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00111100,B01000000,B01000000,B01111100,B00000000,//u\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00011100,B00100000,B01000000,B00100000,B00011100,//v\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B00111100,B01000000,B00111100,B01000000,B00111100,//w\n';
  Blockly.Arduino.definitions_['define_LIST'] += '5,8,B01000100,B00101000,B00010000,B00101000,B01000100,//x\n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B10011100,B10100000,B10100000,B01111100,B00000000,//y\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B01100100,B01010100,B01001100,B00000000,B00000000,//z\n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B00001000,B00110110,B01000001,B00000000,B00000000,//{ \n';
  Blockly.Arduino.definitions_['define_LIST'] += '1,8,B01111111,B00000000,B00000000,B00000000,B00000000,//| \n';
  Blockly.Arduino.definitions_['define_LIST'] += '3,8,B01000001,B00110110,B00001000,B00000000,B00000000,//} \n';
  Blockly.Arduino.definitions_['define_LIST'] += '4,8,B00001000,B00000100,B00001000,B00000100,B00000000,//~ \n';
  Blockly.Arduino.definitions_['define_LIST'] += '};'
  Blockly.Arduino.definitions_['define2_MaxMatrix'] = 'MaxMatrix m(' + pin_din + ',' + pin_cs + ',' + pin_clk + ',' + lc_num + ');\nbyte buffer[100];';
  Blockly.Arduino.setups_['setup_init'] = ' m.init(); ';
  Blockly.Arduino.setups_['setup_Intensity'] = 'm.setIntensity(' + Intensity + ');';
  var code = '';
  return code;
};

//显示-MAX7219-显示字符串
Blockly.Arduino.mg_MAX7219_putString = function() {
  var str = Blockly.Arduino.valueToCode(this, 'String', Blockly.Arduino.ORDER_ATOMIC) || '\"\"';
  var speed = Blockly.Arduino.valueToCode(this, 'Speed', Blockly.Arduino.ORDER_ATOMIC);
  var code = '';
  if (str.charAt(0) == '"') {
    Blockly.Arduino.definitions_['define_message'] = 'char message[] = ' + str + ';';
  } else {
    Blockly.Arduino.definitions_['define_message'] = 'char message[100];';
    code = str + '.toCharArray(message,100);\n';
  }
  Blockly.Arduino.definitions_['define_putChar'] = 'void putChar(char c, int scrollspeed)\n{\nif (c < 32 || c > 127) \nreturn;\nc -= 32;\nmemcpy_P(buffer, LIST + 7*c, 7);\n  m.writeSprite(64, 0, buffer);\nm.setColumn(64 + buffer[0], 0);\nfor (int i=0; i<buffer[0]+1; i++)\n {\ndelay(scrollspeed);\nm.shiftLeft(false, false);\n}\n}';
  Blockly.Arduino.definitions_['define_putString'] = 'void putString(char* s, int scrollspeed)\n{\nwhile (*s != 0)\n{\nputChar(*s, scrollspeed);\ns++;\n}\n}';
  code += 'putString(message, ' + speed + ');\n';
  return code;
};

//显示-max7219-显示图案 
Blockly.Arduino.mg_MAX7219_DisplayChar = function() {
  //var lc_num = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
  //  var lc_num=1;
  var code;
  var lc_chars = Blockly.Arduino.valueToCode(this, 'Chars', Blockly.Arduino.ORDER_ATOMIC);
  code = 'for (int i = 0; i < 8; i++)\n';
  code += ' m.setColumn(i, ' + lc_chars + '[i]);\n';
  return code;
};


//显示-max7219点阵选择数组
Blockly.Arduino.mg_LedArray = function() {
  var varName = this.getFieldValue('VAR');
  var a = new Array();
  for (var i = 1; i < 9; i++) {
    a[i] = new Array();
    for (var j = 1; j < 9; j++) {
      a[i][j] = (this.getFieldValue('a' + i + j) == "TRUE") ? 1 : 0;
    }
  }
  var code = '{';
  for (var i = 1; i < 9; i++) {
    var tmp = ""
    for (var j = 1; j < 9; j++) {
      tmp += a[i][j];
    }
    tmp = (parseInt(tmp, 2)).toString(16)
    if (tmp.length == 1) tmp = "0" + tmp;
    code += '0x' + tmp + ((i != 8) ? ',' : '');
  }
  code += '};\n';
  //Blockly.Arduino.definitions_[this.id] = "byte LedArray_"+clearString(this.id)+"[]="+code;
  Blockly.Arduino.definitions_[varName] = "byte " + varName + "[]=" + code;
  //return ["LedArray_"+clearString(this.id), Blockly.Arduino.ORDER_ATOMIC];
  return [varName, Blockly.Arduino.ORDER_ATOMIC];
};

//显示-max7219-选择图案
Blockly.Arduino.mg_Max7219_img = function() {
  var dropdown_img_ = this.getFieldValue('img_');
  var code = '"' + dropdown_img_ + '"';
  code = '{';
  for (var i = 0; i < 15; i += 2) {
    code += '0x' + dropdown_img_.substr(i, 2) + ((i != 14) ? ',' : '');
  }
  code += '};\n';
  Blockly.Arduino.definitions_['max7219_img_' + dropdown_img_] = "byte " + 'max7219_img_' + dropdown_img_ + "[]=" + code;
  return ['max7219_img_' + dropdown_img_, Blockly.Arduino.ORDER_ATOMIC];
};


//TODO 红外发射接收
Blockly.Arduino.mg_ir_recv = function () {
  var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
  var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
  if(xmlText.indexOf("type=\"controls_tone\"") == -1 && xmlText.indexOf("type=\"controls_notone\"") == -1)
  {
    this.setWarningText(null);
  }
  else
  {
    this.setWarningText(Blockly.IR_AND_TONE_WARNING);
  }

  var variable = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  Blockly.Arduino.definitions_['var_declare' + variable] = 'long ' + variable + ';';
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var branch = Blockly.Arduino.statementToCode(this, 'DO');
  var branch2 = Blockly.Arduino.statementToCode(this, 'DO2');
  var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'),
   Blockly.Variables.NAME_TYPE);
  Blockly.Arduino.definitions_['include_IRremote'] = '#include <MG_IRremote.h>\n';
    //Blockly.Arduino.definitions_['var_declare'+varName] = 'long '+varName+';\n';
    Blockly.Arduino.definitions_['var_declare_ir_recv' + dropdown_pin] = 'IRrecv irrecv_' + dropdown_pin + '(' + dropdown_pin + ');\ndecode_results results_' + dropdown_pin + ';\n';
    Blockly.Arduino.setups_['setup_ir_recv_' + dropdown_pin] = 'irrecv_' + dropdown_pin + '.enableIRIn();';
    var code = "if (irrecv_" + dropdown_pin + ".decode(&results_" + dropdown_pin + ")) {\n"
    code += '  ' + variable + '=results_' + dropdown_pin + '.value;\n';
    code += '  String type="UNKNOWN";\n';
    code += '  String typelist[18]={"UNUSED", "RC5", "RC6", "NEC", "SONY", "PANASONIC", "JVC", "SAMSUNG", "WHYNTER", "AIWA_RC_T501", "LG", "SANYO", "MITSUBISHI", "DISH", "SHARP", "DENON", "PRONTO", "LEGO_PF"};\n';
    code += '  if(results_' + dropdown_pin + '.decode_type>=1&&results_' + dropdown_pin + '.decode_type<=17){\n';
    code += '    type=typelist[results_' + dropdown_pin + '.decode_type];\n'
    code += '  }\n';
    code += '  Serial.println("IR TYPE:"+type+"  ");\n';
    code += branch;
    code += '  irrecv_' + dropdown_pin + '.resume();\n'
    code += '} else {\n';
    code += branch2;
    code += '}\n';
    return code;
  };


  Blockly.Arduino.ir_recv_enable = function () {
    var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
    var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
    if(xmlText.indexOf("type=\"controls_tone\"") == -1 && xmlText.indexOf("type=\"controls_notone\"") == -1)
    {
      this.setWarningText(null);
    }
    else
    {
      this.setWarningText(Blockly.IR_AND_TONE_WARNING);
    }

    Blockly.Arduino.definitions_['include_IRremote'] = '#include <MG_IRremote.h>';
    var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
    var code = 'irrecv_' + dropdown_pin + '.enableIRIn();\n';
    return code;
  }

  Blockly.Arduino.ir_send_nec = function () {
    Blockly.Arduino.definitions_['include_IRremote'] = '#include <MG_IRremote.h>\n';
    Blockly.Arduino.definitions_['var_declare_ir_send'] = 'IRsend irsend;\n';
    var data = Blockly.Arduino.valueToCode(this, 'data', Blockly.Arduino.ORDER_ATOMIC) || '0';
    var bits = Blockly.Arduino.valueToCode(this, 'bits', Blockly.Arduino.ORDER_ATOMIC) || '0';
    var type = this.getFieldValue('TYPE');
    var code = 'irsend.send' + type + '(' + data + ',' + bits + ');\n';
    /*
	for (var name in Blockly.Arduino.definitions_) {
		var def = Blockly.Arduino.definitions_[name];
		if (def.match(/^IRrecv irrecv_/)) {
			var tmp=def.substring(7,def.indexOf('('));
			code=code+tmp+'.enableIRIn();\n';
		}
	}*/
  return code;
}

Blockly.Arduino.mg_ir_recv_raw = function () {
  var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
  var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
  if(xmlText.indexOf("type=\"controls_tone\"") == -1 && xmlText.indexOf("type=\"controls_notone\"") == -1)
  {
    this.setWarningText(null);
  }
  else
  {
    this.setWarningText(Blockly.IR_AND_TONE_WARNING);
  }

  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.definitions_['include_IRremote'] = '#include <MG_IRremote.h>\n';
  Blockly.Arduino.definitions_['var_declare_ir_recv' + dropdown_pin] = 'IRrecv irrecv_' + dropdown_pin + '(' + dropdown_pin + ');\n ';
  if (Blockly.Arduino.setups_['setup_serial_Serial' + profile.default.serial]) {
  } else {
    Blockly.Arduino.setups_['setup_serial_Serial' + profile.default.serial] = 'Serial.begin(' + profile.default.serial + ');';
  }
  Blockly.Arduino.setups_['setup_ir_recv_' + dropdown_pin] = 'irrecv_' + dropdown_pin + '.enableIRIn();\n';
  var code = "decode_results results_"+ dropdown_pin +";\n if (irrecv_" + dropdown_pin + ".decode(&results_" + dropdown_pin + ")) {\n"
  code += '  ' + 'dumpInfo(&results_' + dropdown_pin + ');\n';
  code += '  ' + 'dumpRaw(&results_' + dropdown_pin + ');\n';
  code += '  ' + 'dumpCode(&results_' + dropdown_pin + ');\n';
  code += '  ' + 'Serial.println("");\n';
  code += '  irrecv_' + dropdown_pin + '.resume();\n'
  code += '}\n';
  var ircode = 'void ircode(decode_results *results) {\n'
  + '  if (results->decode_type == PANASONIC) {\n'
  + '    Serial.print(results->address, HEX);\n'
  + '    Serial.print(":");\n'
  + '  }\n'
  + '  Serial.print(results->value, HEX);\n'
  + '}\n';
  Blockly.Arduino.definitions_['ircode'] = ircode;
  var encoding = 'void encoding(decode_results *results) {\n'
  + '  switch (results->decode_type) {\n'
  + '    default:\n'
  + '    case UNKNOWN:      Serial.print("UNKNOWN");\n break ;\n'
  + '    case NEC:      Serial.print("NEC");\n break ;\n'
  + '    case SONY:      Serial.print("SONY");\n break ;\n'
  + '    case RC5:      Serial.print("RC5");\n break ;\n'
  + '    case RC6:      Serial.print("RC6");\n break ;\n'
  + '    case DISH:      Serial.print("DISH");\n break ;\n'
  + '    case SHARP:      Serial.print("SHARP");\n break ;\n'
  + '    case JVC:      Serial.print("JVC");\n break ;\n'
  + '    case PANASONIC:      Serial.print("PANASONIC");\n break ;\n'
  + '  }\n'
  + '}\n';
  Blockly.Arduino.definitions_['encoding'] = encoding;
  var dumpInfo = 'void dumpInfo(decode_results *results) {\n'
  + '  if (results->overflow) {\n'
  + '    Serial.println("IR code too long. Edit IRremoteInt.h and increase RAWLEN");\n'
  + '    return;\n'
  + '  }\n'
  + '  Serial.print("Encoding  : ");\n'
  + '  encoding(results);\n'
  + '  Serial.println("");\n'
  + '  Serial.print("Code      : ");\n'
  + '  ircode(results);\n'
  + '  Serial.print(" (");\n'
  + '  Serial.print(results->bits, DEC);\n'
  + '  Serial.println(" bits)");\n'
  + '}\n';
  Blockly.Arduino.definitions_['dumpInfo'] = dumpInfo;
  var dumpRaw = 'void dumpRaw(decode_results *results) {\n'
  + '  Serial.print("Timing[");\n'
  + '  Serial.print(results->rawlen-1, DEC);\n'
  + '  Serial.print("]: ");\n'
  + '  for (int i = 1;  i < results->rawlen;  i++) {\n'
  + '    unsigned long  x = results->rawbuf[i] * USECPERTICK;\n'
  + '    if(!(i & 1)){\n'
  + '      Serial.print("-");\n'
  + '      if (x < 1000)  Serial.print(" ") ;\n'
  + '      if (x < 100)   Serial.print(" ") ;\n'
  + '      Serial.print(x, DEC);\n'
  + '    } else {\n'
  + '      Serial.print("     ");\n'
  + '      Serial.print("+");\n'
  + '      if (x < 1000)  Serial.print(" ") ;\n'
  + '      if (x < 100)   Serial.print(" ") ;\n'
  + '      Serial.print(x, DEC);\n'
  + '      if (i < results->rawlen-1) Serial.print(", ");\n'
  + '    }\n'
  + '    if (!(i % 8))  Serial.println("");\n'
  + '  }\n'
  + '  Serial.println("");\n'
  + '}\n';
  Blockly.Arduino.definitions_['dumpRaw'] = dumpRaw;
  var dumpCode = 'void dumpCode(decode_results *results) {\n'
  + '  Serial.print("unsigned int  ");\n'
  + '  Serial.print("rawData[");\n'
  + '  Serial.print(results->rawlen - 1, DEC);\n'
  + '  Serial.print("] = {");\n'
  + '  for (int i = 1;  i < results->rawlen;  i++) {\n'
  + '    Serial.print(results->rawbuf[i] * USECPERTICK, DEC);\n'
  + '    if(i < results->rawlen-1){\n'
  + '      Serial.print(",");\n'
  + '    }\n'
  + '    if (!(i & 1))  Serial.print(" ");\n'
  + '  }\n'
  + '  Serial.print("};");\n'
  + '  Serial.print("  // ");\n'
  + '  encoding(results);\n'
  + '  Serial.print(" ");\n'
  + '  ircode(results);\n'
  + '  Serial.println("");\n'

  + '  if (results->decode_type != UNKNOWN) {\n'
  + '    if(results->decode_type == PANASONIC){\n'
  + '      Serial.print("unsigned int  addr = 0x");\n'
  + '      Serial.print(results->address, HEX);\n'
  + '      Serial.println(";");\n'
  + '    }\n'
  + '    Serial.print("unsigned int  data = 0x");\n'
  + '    Serial.print(results->value, HEX);\n'
  + '    Serial.println(";");\n'
  + '  }\n'
  + '}\n';
  Blockly.Arduino.definitions_['dumpCode'] = dumpCode;
  return code;
};

Blockly.Arduino.mg_ir_send_raw = function () {
  Blockly.Arduino.definitions_['include_PROGMEM'] = '#include <avr/pgmspace.h>\n';
  Blockly.Arduino.definitions_['include_IRremote'] = '#include <MG_IRremote.h>\n';
  Blockly.Arduino.definitions_['var_declare_ir_send'] = 'IRsend irsend;\n';
  var length = Blockly.Arduino.valueToCode(this, 'length', Blockly.Arduino.ORDER_ATOMIC) || '0';
  Blockly.Arduino.definitions_['var_declare_buf_raw'] = 'unsigned int buf_raw[' + length + '];\n';
  var text = this.getFieldValue('TEXT');
  var suffix = Math.floor(Math.random() * 10000) + 1;
  Blockly.Arduino.definitions_['buf_raw_suffix'+ suffix] = 'const unsigned int buf_raw_'+suffix+'[' + length + '] PROGMEM ={' + text + '};\n';
  var freq = Blockly.Arduino.valueToCode(this, 'freq', Blockly.Arduino.ORDER_ATOMIC) || '0';
  var code = 'for(int i = 0;i<' + length + ';i++){\n'
  +'    buf_raw[i]=pgm_read_word_near(buf_raw_' + suffix + '+i);\n'
  +'  }\n'
  +'irsend.sendRaw(buf_raw,' + length + ',' + freq + ');\n';
  return code;
}


//执行器-蜂鸣器频率选择列表
Blockly.Arduino.MAKER17_tone_notes = function() {
  var code = this.getFieldValue('STAT');
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

//执行器-蜂鸣器
Blockly.Arduino.MAKER17_tone = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var fre = Blockly.Arduino.valueToCode(this, 'FREQUENCY', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
  var dur = Blockly.Arduino.valueToCode(this, 'DURATION', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
  Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  Blockly.Arduino.definitions_['newtone0'] = 'float period;\nfloat pulse;';
  Blockly.Arduino.definitions_['newtone1'] = 'void newtone(int tonePin, int frequency, int duration)';
  Blockly.Arduino.definitions_['newtone2'] = '{\nfloat period = 1000000.0 /frequency;';
  Blockly.Arduino.definitions_['newtone3'] = 'float pulse = period / 2.0;';
  Blockly.Arduino.definitions_['newtone4'] = 'for (int i=1; i<=((duration * 1000.0)/period);i=i+1)';
  Blockly.Arduino.definitions_['newtone5'] = ' {\npinMode(tonePin, OUTPUT);\n digitalWrite(tonePin,HIGH);';
  Blockly.Arduino.definitions_['newtone6'] = 'delayMicroseconds(pulse);';
  Blockly.Arduino.definitions_['newtone7'] = 'pinMode(tonePin, OUTPUT);\n digitalWrite(tonePin,LOW);';
  Blockly.Arduino.definitions_['newtone8'] = ' delayMicroseconds(pulse);\n}\n}\n';
  var code = "newtone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
  return code;
};

//执行器-蜂鸣器结束声音
Blockly.Arduino.MAKER17_newNoTone = function() {
  var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  var Delay_time = Blockly.Arduino.valueToCode(this, 'DELAY_TIME', Blockly.Arduino.ORDER_ATOMIC);
  Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
  var code = "digitalWrite(" + dropdown_pin + ", LOW);\n";
  code += 'delay(' + Delay_time + ');';
  return code;
};